import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useLanguageStore = defineStore('language', () => {
  // 当前语言
  const currentLanguage = ref('zh_cn')

  // 支持的语言列表
  const supportedLanguages = ref([
    { code: 'zh_cn', name: '中文', nativeName: '简体中文' },
    { code: 'en_us', name: 'English', nativeName: 'English' },
    { code: 'ja_jp', name: '日本語', nativeName: '日本語' },
    { code: 'be_by', name: 'Беларуская', nativeName: 'Беларуская мова' }
  ])

  // 设置语言
  const setLanguage = (langCode) => {
    currentLanguage.value = langCode
    localStorage.setItem('language', langCode)
  }

  // 初始化语言（从localStorage或默认）
  const initLanguage = () => {
    const savedLang = localStorage.getItem('language')
    if (savedLang && supportedLanguages.value.find(lang => lang.code === savedLang)) {
      currentLanguage.value = savedLang
    }
  }

  // 获取语言显示名称
  const getLanguageName = (langCode) => {
    const lang = supportedLanguages.value.find(l => l.code === langCode)
    return lang ? lang.nativeName : langCode
  }

  return {
    currentLanguage,
    supportedLanguages,
    setLanguage,
    initLanguage,
    getLanguageName
  }
})